package tm.data;

import java.sql.DriverManager;
import java.sql.SQLException;

import java.sql.Connection;
import java.sql.PreparedStatement;

public abstract class MySQLJDBCGenericDao {

	String user = "cs673";
	String password = "cs673";

	Connection conn = null;
	String dbConnectString = "jdbc:mysql://localhost:3306/cs673";
	
	public MySQLJDBCGenericDao(){
		super();
		initialize();
	}
	private void initialize(){
		try {
	        Class.forName("com.mysql.jdbc.Driver");
	    }catch(ClassNotFoundException cnfe){
	    	System.out.println("Class Not Found Exception thrown..");
	    }
	    
	    try{
	    	conn = DriverManager.getConnection(dbConnectString, user, password);
	    	conn.setAutoCommit(true);
	    	
	    }catch(SQLException se){
	    	System.out.println("SQL Exception thrown..");
	    }catch(Exception e){
	    	e.printStackTrace(System.out);
	    }
	}
	
	public void close() throws SQLException {
		PreparedStatement ps = getConn().prepareStatement("SHUTDOWN");
		ps.execute();
		if(conn!=null){
			conn.close();
			conn = null;
		}
			
	}
	public void reconnect() throws SQLException, ClassNotFoundException{
		if (conn != null)
			conn.close();
		initialize();	
	}
	
	/**
	 * @return Returns the conn.
	 */
	public Connection getConn() {
		return conn;
	}

	
}

